//
//  BorrowingColorVC.m
//  BrushFace
//
//  Created by timer_q on 15/12/12.
//  Copyright © 2015年 Zzz. All rights reserved.
//

#import "BorrowingColorVC.h"
#import "BorrowDetailRequest.h"
#import "RadarView.h"
#import "AppStatus.h"
#import "BarrageView.h"
#import "ARRequestModel.h"
#import "NavHelper.h"
@interface BorrowingColorVC ()
{
    UIImageView *_statuImage;
    UILabel *numlabel;
}
@property (nonatomic,strong)RadarView *radarView;
@property (nonatomic,strong)BorrowDetailRequest *request;

@end

@implementation BorrowingColorVC

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    [_request getBorrowDetailWithHopeId:self.hopeid andCB:^(id output)
    {
        _radarView.dataDic = (NSDictionary *)output;
       // NSLog(@"harry---%@",output);

    }];
    if (self.radarView!=nil) {
        [self.radarView addHopeidToView:self.hopeid];
        [self.radarView radarViewStartScan];
    }
    [ARRequestModel sharedInstance].hopeId = self.hopeid;
    [[ARRequestModel sharedInstance] fetchPingLunListAndCB:^(id output) {
        [self createBarrageWithDataArray:[output objectForKey:@"data"]];
        numlabel.text = [NSString stringWithFormat:@"%@弹幕",[output objectForKey:@"totalRecords"]];
    }];
}

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [self.radarView radarViewStopScan];
    [self.radarView stopTimer];
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
     self.navBar.backgroundColor = [UIColor clearColor];
    [self.navBar setLeftBtn:NAV_BAR_LEFT_BTN_TYPE_WHITE];
    [self.navBar setTitle:@"借款中"];
    [self.navBar setTintColor:[UIColor whiteColor]];
    
    _request = [[BorrowDetailRequest alloc] init];
    [self creatRadarView];
    [self addRightButton];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(startRadar) name:@"startRadar" object:nil];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(stopRadar) name:@"stopRadar" object:nil];
}
-(void)stopRadar
{
    [self.radarView radarViewStopScan];

}
-(void)startRadar
{
    [self.radarView radarViewStartScan];

}
-(void)creatRadarView
{
    _radarView = [[RadarView alloc]initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT-64)];
    
    [self.view addSubview:_radarView];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - 右上角弹幕开关
- (void)addRightButton{
    UIView *rightBackV = [[UIView alloc] initWithFrame:CGRectMake(0, 20, 100, 44)];
    rightBackV.backgroundColor = [UIColor clearColor];
    
    _statuImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 12, 20, 20)];
    _statuImage.image = [UIImage imageNamed:@"danmuOpen"];
    _statuImage.contentMode = UIViewContentModeScaleToFill;
    [rightBackV addSubview:_statuImage];
    numlabel = [[UILabel alloc] initWithFrame:CGRectMake(30, 13, 55, 18)];
    numlabel.backgroundColor = ColorDefaultOrange;
    numlabel.layer.cornerRadius = 4;
    numlabel.clipsToBounds = YES;
    numlabel.textColor = [UIColor whiteColor];
    numlabel.textAlignment = NSTextAlignmentCenter;
    numlabel.font = [UIFont systemFontOfSize:10];
    numlabel.text = @"37弹幕";
    [rightBackV addSubview:numlabel];
    
    UIButton *danmuBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0,_statuImage.frame.size.width , rightBackV.frame.size.height)];
    [danmuBtn addTarget:self action:@selector(danmuBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    danmuBtn.selected = YES;
    [rightBackV addSubview:danmuBtn];
    [self.navBar setRightView:rightBackV];
    
    UIButton *msgbtn = [[UIButton alloc] initWithFrame:CGRectMake(numlabel.frame.origin.x, 0,numlabel.frame.size.width , rightBackV.frame.size.height)];
    [msgbtn addTarget:self action:@selector(gotoMsgView:) forControlEvents:UIControlEventTouchUpInside];
    [rightBackV addSubview:msgbtn];
    [self.navBar setRightView:rightBackV];
}
- (void)danmuBtnClick:(UIButton *)sender{
    if (sender.selected == NO) {
        sender.selected = YES;
        _statuImage.image = [UIImage imageNamed:@"danmuOpen"];_barrageView.hidden = NO;
    }else{
        _statuImage.image = [UIImage imageNamed:@"danmuClose"];
        sender.selected = NO;
        _barrageView.hidden = YES;
    }
}
- (void)gotoMsgView:(UIButton *)sender{
    [NavHelper gotoSendMsgVCWithHopeID:self.hopeid];
    
}
#pragma mark - action

-(void)leftButtonClick{
    if ([AppStatus sharedInstance].radarBackType == ENUM_BORROW_IN ) {
        [self.navigationController popViewControllerAnimated:YES];
    }else{
        [self.navigationController popToRootViewControllerAnimated:YES];
    }
}

#pragma mark - 弹幕
static BarrageView * _barrageView;
- (void)createBarrageWithDataArray:(NSArray *)dataArr{
    if (dataArr.count == 0) {
        return;
    }
    if (!_barrageView) {
        [_barrageView removeFromSuperview];
        _barrageView = nil;
    }
    
    _barrageView = [[BarrageView alloc]initWithFrame:CGRectMake(0, SCREEN_HEIGHT-300, SCREEN_WIDTH, 200)];
    
    [self.radarView addSubview:_barrageView];
    [self.radarView bringSubviewToFront:_radarView.alreadyBorrowView];
    NSMutableArray *barrageArr = [[NSMutableArray alloc]init];
    for (NSInteger index = 0;  index<dataArr.count; index ++) {
        NSDictionary *subDict = dataArr[index];
        BarrageItemModel * itemModel = [[BarrageItemModel alloc]init];
        itemModel.uid = subDict[@"friendid"];
        [itemModel setContentStr:subDict[@"content"]];
        [itemModel setPlayTime:(arc4random() % 4 + 4)];
        
        NSInteger type = [[NSString stringWithFormat:@"%@",subDict[@"type"]] integerValue];
        if (type==3) {
            [itemModel setIsShowRedSpecial:1];
        }else{
            [itemModel setIsShowRedSpecial:0];
        }
        NSInteger level = [[NSString stringWithFormat:@"%@",subDict[@"level"]] integerValue];
        if (level==1) {
            [itemModel setRedSpecialContent:[NSString stringWithFormat:@"我的朋友：出借%@",subDict[@"amount"]]];
        }else{
            [itemModel setRedSpecialContent:[NSString stringWithFormat:@"朋友的朋友：出借%@",subDict[@"amount"]]];
        }
        [barrageArr addObject:itemModel];
    }
    [_barrageView setPlayTime:30];
    [_barrageView setBrrageDataArr:barrageArr];
    [_barrageView playAnimation];
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}
@end
